package com.ehl.capture.atest;

import com.ehl.capture.db.RedisUtil;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author Jiax
 * @version 1.0.0
 * @description: JedisTest
 * @date 2024-05-18 07:43
 */
public class JedisTest {

    public static void main(String[] args) {
        Jedis jedis = getJedis();
        teststring(jedis);

        closeJedis(jedis);
    }

    public static Jedis getJedis() {
        //1.建立连接
        Jedis jedis= RedisUtil.getJedis("127.0.0.1",6379,null);
        //2.设置密码
        //jedis.auth("123456");
        //3.选择数据库
        //jedis.select(1);
        return jedis;
    }

    //测试string
    public static void teststring(Jedis jedis) {
        //存入数据
        jedis.set("name", "小花");
        String name = jedis.get("name");
        System.out.println("name = " + name);
    }

    //测试string
    public static void teststringTime(Jedis jedis) {
        //存入数据
        jedis.setex("name", 10,"小花");
        String name = jedis.get("name");
        System.out.println("name = " + name);
    }

    //测试hash
    public static void testhash(Jedis jedis) {
        //再user:1中插入name和age的数据
        jedis.hset("user:1", "name", "张三");
        jedis.hset("user:1", "age", "20");
        //获取hash中的值
        Map<String, String> stringStringMap = jedis.hgetAll("user:1");
        System.out.println("stringStringMap = " + stringStringMap);
    }

    //测试list
    public static void testlist(Jedis jedis) {
        jedis.lpush("stu", "张二", "张三");
        jedis.rpush("stu", "张五", "张六");
        List<String> stu = jedis.lrange("stu", 0, 7);
        for (String s : stu) {
            System.out.println("s = " + s);
        }
    }

    //测试set
    public static void testset(Jedis jedis) {
        jedis.sadd("demo", "a", "b", "c", "张三");
        long srem = jedis.srem("demo", "张三");
        System.out.println("srem = " + srem);
        long demo1 = jedis.scard("demo");
        System.out.println("demo1 = " + demo1);
        boolean sismember = jedis.sismember("demo", "张三");
        System.out.println("sismember = " + sismember);
        Set<String> demo = jedis.smembers("demo");
        System.out.println(demo);
    }

    //测试sortedset
    public static void testsored(Jedis jedis) {
        jedis.zadd("student",98,"张一");
        long student = jedis.zcard("student");
        System.out.println("student = " + student);
    }

    public static void closeJedis(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

}
